package com.qf.controller;

import com.qf.annotation.RequiresPermissionsDesc;
import com.qf.pojo.DtsAdmin;
import com.qf.service.DtsAdminService;
import com.qf.util.ResponseUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/** 系统管理 管理员
 * @author bss
 * @version V1.0
 * @Project maven
 * @Package com.qf.controller
 * @Description:
 * @Date 2022/10/14 10:12
 */
@RestController
@RequestMapping("/admin/admin")
@Slf4j
public class AdminAdminController {

    @Autowired
    DtsAdminService adminService;

    @RequiresPermissions("admin:admin:list")
    @RequiresPermissionsDesc(menu = {"系统管理","管理员管理"},button = "查询")
    @GetMapping("/list")
    public Object list(@RequestParam(value = "page",required = false,defaultValue = "1") Integer page ,
                       @RequestParam(value = "limit",required = false,defaultValue = "20") Integer limit,
                       @RequestParam(value = "sort",required = false,defaultValue = "add_time") String sort,
                       @RequestParam(value = "order",required = false,defaultValue = "desc") String order,
                       String username){

        Map<String, Object> data = adminService.findByPage(page,limit,sort,order,username);


        return ResponseUtil.ok(data);
    }

    @RequiresPermissions("admin:admin:create")
    @RequiresPermissionsDesc(menu = { "系统管理", "管理员管理" }, button = "添加")
    @PostMapping("/create")
    public Object create(@RequestBody DtsAdmin admin){
        log.debug("debug日志  :: "+admin);
        adminService.insert(admin);

        return ResponseUtil.ok(admin);
    }

    @RequiresPermissions("admin:admin:update")
    @RequiresPermissionsDesc(menu = { "系统管理", "管理员管理" }, button = "编辑")
    @PostMapping("/update")
    public Object update(@RequestBody DtsAdmin admin){
        log.info("管理员修改 操作  :: "+admin);
        adminService.update(admin);
        log.info("管理员修改成功");
        return ResponseUtil.ok(admin);
    }

    @RequiresPermissions("admin:admin:delete")
    @RequiresPermissionsDesc(menu = { "系统管理", "管理员管理" }, button = "删除")
    @PostMapping("/delete")
    public Object delete(@RequestBody DtsAdmin admin){
        try {
            adminService.delete(admin);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseUtil.fail(-1,e.getMessage());
        }
        return ResponseUtil.ok();
    }
}
